<?php
class Consume extends Integral{
    
	
	/*
	
	public $num 总积分
	
	*/
	
	//初始化
	public function Consume($member_id){
	  $this->isPay = true;
	  $integral_numberDB = M('integral_number'); 
	  $this->data = $integral_numberDB->where(array('member_id'=>$member_id,'typename'=>'Consume'))->find();
	  $this->num = $this->data['number'];
	  $this->name = $this->data['remark'];
	  $this->member_id = $member_id;
	  $this->dao = 'Consume';
		
	}
	
	//一个订单数组抵现结果
	public function integral_pay_array($product,$confirm,$num){
	   
	   $money = 0;
	   $integral = 0;
	   $integralsum = 0;
	   $i = 0;
	   if($num == 0 && $confirm === false){
	      $integral = 1000000;
		  
	   }elseif($num > $this->num){
		   
	     $integral = $this->num;
		 
	   }else{
		   
	     $integral = $num;
	   }
	  
	   foreach($product as $key=>$value){
	       
		   
		   //是否抵现
		   if(!$this->is_integral_pay($value['id'])){
			  continue;
		   }
		   
		   //积分不足
		   if($this->num < 0){

			 break;
		   
		   }elseif($integral <= 0){
		      
			 break;
		   }
		   
		   //执行抵现
		   $datas = $this->integral_pay($value,$integral);
		   
		   
		   
		   //统计积分金额
		   $money = $money+$datas['money'];
		   $integralsum = $integralsum +$datas['integral'];
		   $integral = $integral-$datas['integral'];
	       $i++;
	   }
	   // dump($integralsum);
	   
	   //判断是否更新数据
	   if($confirm){
	     $mes = $this->upIntegral($integralsum);
		 if($mes){
		   $remark = '积分抵现，消费:'.$integralsum.'积分,抵现金额:'.$money;
		   parent::integral_log($this->member_id,$remark,$integralsum);  
		 }
	   }
	   
	   if($i == 0){
	      return false;
	   }
	   
	   if($num == 0){
	     $data['money'] = 0;
	     $data['integralsum'] = $integralsum;
	   }else{
	     $data['money'] = $money;
	     $data['integralsum'] = $integralsum;
	   }
	   	 
	   return $data;
		
    }
    
	
	//计算能否抵现
	public function is_integral_pay($pid){
		
		$integral_payDB = M('integral_pay');
	    $integral_pay = $integral_payDB->where(array('product_id'=>$pid))->find();
		
		if(!$integral_pay){
		    return false;
		}else{
		    return true;
		}
		
	    
	}
	
	
	
	//计算抵现
	public function integral_pay($value,$num){
	  
	  $integral_payDB = M('integral_pay');
	  $integral_pay = $integral_payDB->where(array('product_id'=>$value['id']))->find();
	  
	  if($num >= $integral_pay['intergralnum']*$value['num']){
	     $data['money'] = $value['price']*$value['num'];
		 $data['integral'] = $integral_pay['intergralnum']*$value['num'];
	  }
	  
	  if($num < $integral_pay['intergralnum']*$value['num']){
	     
		  $data['money'] =  $value['price']*$value['num']-($value['price']*($integral_pay['intergralnum']*$value['num']-$num)/$integral_pay['intergralnum']); 
		  $data['money'] = round($data['money'],2);
		  $data['integral'] = $num;

	  }
	 
      
	  return $data;
		   
	}
	
	//更新积分
	public function upIntegral($integralsum){

	    $integral_numberDB = M('Integral_number');
		$member_id = $this->member_id;
		$integral_number = $integral_numberDB->where(array('member_id'=>$member_id,'typename'=>'Consume'))->find();
		$integral_number['number']  = $integral_number['number']-$integralsum;
		$mes = $integral_numberDB->where(array('member_id'=>$member_id,'typename'=>'Consume'))->save(array('number'=>$integral_number['number']));
        //dump($integralsum);
	    if($mes){
		   
		   return true;
		 
		 }else{
		   
		   return false;
		 
		 }
	
	}


}